<template>
  <div class="login-container">
    <el-card class="login-main">
      <div class="title"><span>vite-drag</span></div>
      <el-form
          class="login-main-form"
          ref="loginFormRef"
          :model="loginForm"
          label-placement="left"
          label-align="right"
          :label-width="0"
      >
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
                    @keyup.enter.native="handleLogin">
            <template #prefix>
              <i class="iconfont icon-yonghu"></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
                    show-password
                    @keyup.enter.native="handleLogin">
            <template #prefix>
              <i class="iconfont icon-mima"></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="captcha" class="captcha-form">
          <el-input v-model="loginForm.captcha"
                    type="text"
                    @keyup.enter.native="handleLogin">
          </el-input>
          <div v-html="captcha" class="captcha" @click="getCaptcha"></div>
        </el-form-item>
        <el-form-item>
          <el-button class="login-main-form-btn" plain type="success" @click="handleLogin" v-loading="loginLoding">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { defineComponent, onMounted, ref } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { getCaptchaAPI } from "./../../service/user";

export default defineComponent({
  name: "Login",
  setup() {
    const loginForm = ref({
      username: "admin",
      password: "admin",
      captcha: ""
    });
    const loginLoding = ref(false);
    const captcha = ref();
    const rules = ref({
      username: {
        required: true,
        message: "请输入用户名",
        trigger: "blur"
      },
      password: {
        required: true,
        message: "请输入密码",
        trigger: ["input", "blur"]
      }
    });
    const loginFormRef = ref();
    const store = useStore();
    const router = useRouter();
    const handleLogin = () => {
      loginLoding.value = true
      loginFormRef.value.validate((valid) => {
        if (valid) {
          store.dispatch("user/LoginByUsername", loginForm.value).then((res) => {
            loginLoding.value = false
            router.push("/project/list");
          }).catch(err => {
            loginLoding.value = false
            getCaptcha();
          });
        } else {
          loginLoding.value = false
          getCaptcha();
        }
      });
    };
    const getCaptcha = () => {
      getCaptchaAPI().then(res => {
        captcha.value = res.data.data.result;
      });
    };
    onMounted(() => {
      getCaptcha();
    });
    return {
      loginLoding,
      loginForm,
      rules,
      loginFormRef,
      captcha,
      handleLogin,
      getCaptcha
    };
  }
});
</script>

<style scoped lang="less">
@import "./../../style/page/login.less";
</style>
